package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.LoginCode;
import com.qian.api.ddj.service.ILoginCodeService;
import com.qian.service.ddj.dao.LoginCodeRepository;
import com.qian.service.ddj.entity.DdjLoginCode;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class LoginCodeServiceImpl extends BaseServiceImpl<LoginCode, DdjLoginCode, Integer> implements ILoginCodeService {

    @Autowired
    private LoginCodeRepository dao;

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjLoginCode model2Entity(LoginCode model) {
        DdjLoginCode ddjLoginCode = null;
        if (isNotEmpty(model)) {
            ddjLoginCode = new DdjLoginCode();
            ddjLoginCode.setAccount(model.getAccount());
            ddjLoginCode.setCode(model.getCode());
            ddjLoginCode.setRemark(model.getRemark());
            ddjLoginCode.setStatus(model.getStatus());
            ddjLoginCode.setId(model.getId());
        }
        return ddjLoginCode;
    }

    @Override
    protected LoginCode entity2model(DdjLoginCode entity) {
        LoginCode loginCode = null;
        if (isNotEmpty(entity)) {
            loginCode = new LoginCode();
            loginCode.setStatus(entity.getStatus());
            loginCode.setCode(entity.getCode());
            loginCode.setRemark(entity.getRemark());
            loginCode.setAccount(entity.getAccount());
            loginCode.setCreateTime(entity.getCreateTime());
            loginCode.setId(entity.getId());
        }
        return loginCode;
    }

    @Override
    @Transactional
    public LoginCode save(LoginCode model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            DdjLoginCode ddjLoginCode = dao.findOne(model.getId());
            if (isNotEmpty(model.getCode()))
                ddjLoginCode.setCode(model.getCode());
            if (isNotEmpty(model.getStatus()))
                ddjLoginCode.setStatus(model.getStatus());
            model = entity2model(dao.save(ddjLoginCode));
        }
        return model;
    }
}
